// Implements a simple, cross-browser interface to the XMLHttpRequest object.
// Parameters:
//
// - url: the URL to request
// 
// - onsuccess: a function that is called when the request has successfully
//   completed. The function should take two parameters:
//   - responseXML (defined only if the response is well-formed XML)
//   - responseText
// 
// - onfailure: a function that is called if the HTTP request fails, e.g. if an
//   invalid URL is passed to it. It should take two parameters:
//   - status: the HTTP status code
//   - statusText: a brief description of the error
function simpleXMLHttpRequest(url, onsuccess, onfailure) {
    var STATE_UNINITIALIZED = 0;
    var STATE_LOADING = 1;
    var STATE_LOADED = 2;
    var STATE_INTERACTIVE = 3;
    var STATE_COMPLETE = 4;
    var STATUS_OK = 200;
    var STATUS_NOT_MODIFIED = 304;

    // Stolen from http://jibbering.com/2002/4/httprequest.2004.html

    var xmlhttp = null;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (f) {
            xmlhttp = null;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }

    if (!xmlhttp) {
        return false;
    } else {
        xmlhttp.onreadystatechange = function () {
            if (STATE_COMPLETE == xmlhttp.readyState) {
                if (STATUS_OK == xmlhttp.status || STATUS_NOT_MODIFIED == xmlhttp.status) {
                    onsuccess(xmlhttp.responseXML, xmlhttp.responseText);
                } else {
                    onfailure(xmlhttp.status, xmlhttp.statusText);
                }
            }
        };

        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
        return true;
    }
};

function synchronousHttpRequest(url) {
    var xmlhttp = null;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (f) {
            xmlhttp = null;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }

    if (!xmlhttp) {
        return false;
    } else {
	xmlhttp.open("GET", url, false);
	xmlhttp.send(null);
	return xmlhttp.responseText;
    }
}

var gadgets = gadgets || {};
gadgets.window = gadgets.window || {};
gadgets.util = gadgets.util || {};
gadgets.views = gadgets.view || {};

var onloaders = new Array();

function _gel(el) {
	return document.getElementById(el);
};

function NullFunction() {
	return;
};

function twoParamNullFunction(a, b) {
	return;
};

function _IG_FetchContent(url, funct) {
	simpleXMLHttpRequest('../prxy/prxy.php?url='+escape(url), function(x,t) {funct(t);}, twoParamNullFunction);
};

gadgets.util.registerOnLoadHandler = function (f) {
	onloaders.push(f);
};

function whatsits_onloads() {
	while (onloaders.length > 0) {
		f = onloaders.pop();
		f();
	}
};

function _esc(str) {
    return escape(str);
};

function _unesc(str) {
    return unescape(str);
};

function _trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
};

function _gelstn(tagname) {
    return document.getElementsByTagName(tagname);
};

function _up(str) {
    return str.toUpperCase();
};

function _min(val1, val2) {
    return Math.min(val1, val2);
};

function _max(val1, val2) {
    return Math.max(val1, val2);
};

function _IG_GetImageURL(url) {
//TODO: return a url that comes through a cached mechanism to reduce load on service's server
    return url;
};

function _IG_Prefs(id) {
    this.prefArray = initial_prefs;

    simpleXMLHttpRequest('../ajax/prefs.json.php?id='+id, 
    					 function(x,t) {
    					 	this.prefArray = eval('('+t+')');
    					 	this.prefsLoaded = 1;
    					 },
    					 function(x,t) {
    					 	this.prefsLoaded = -1;
    					 });

    this.getString = function (userpref) {
    	if (this.prefArray[userpref]) {
        	return this.prefArray[userpref]['value'].toString();
    	} else return '';
    };

    this.getInt = function (userpref) {
    	if (this.prefArray[userpref]) {
        	return parseInt(this.prefArray[userpref]['value']);
    	} else return 0;
    };

    this.getBool = function (userpref) {
    	if (this.prefArray[userpref]) {
        	val = this.prefArray[userpref]['value'];
       		if (val == 'true' || val == 'TRUE' || val == 1) {
          		return true;
       		}
    	}
    	return false;
    };

    this.getMsg = function (message_key) {
	url = '../ajax/getmsg.php?id='+id+'&msgid='+message_key;
	return synchronousHttpRequest(url);
    };

    this.set = function (userpref, value) {
        this.prefArray[userpref]['value'] = value;
        simpleXMLHttpRequest('../ajax/setpref.php?key='+userpref+'&value='+value+'&url='+escape(this.url),
						  twoParamNullFunction, 
						  twoParamNullFunction);
    };
};

//Aliases for legacy _IG code
_IG_RegisterOnloadHandler = gadgets.util.registerOnLoadHandler;
//    _IG_RegisterOnloadHandler
